package com.sharp.authorization.service.user.impl;

import com.sharp.authorization.constants.token.TokenItemConst;
import com.sharp.authorization.dao.common.entity.AcUser;
import com.sharp.authorization.dao.common.repository.AcUserService;
import com.sharp.authorization.service.token.TokenService;
import com.sharp.authorization.service.user.UserInfoService;
import com.sharp.authorization.vo.user.FillUserInfoReq;
import com.sharp.authorization.vo.user.RealNameReq;
import com.sharp.authorization.vo.user.UserInfoRes;
import com.sharp.kernel.constant.CommonConst;
import com.sharp.kernel.constant.err.ErrCodeSys;
import com.sharp.kernel.utils.ReflectionUtil;
import com.sharp.framework.utils.security.MD5Util;
import com.sharp.kernel.validate.Validator;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;

/**
 * Title: AcUserService
 * Description: Copyright: Copyright (c) 2019 Company: BHFAE
 *
 * @author Sharp
 * @date 2020/4/3 18:44
 * @description Project Name: Mall
 * @Package: com.srct.service.account.service.impl
 */
@Service
public class UserInfoServiceImpl implements UserInfoService {

    @Resource
    private AcUserService acUserService;
    @Resource
    private TokenService tokenService;

    @Override
    public UserInfoRes getUserInfo() {
        String customerNumber = tokenService.getItem(TokenItemConst.OPERATOR.name(), String.class);
        AcUser user = acUserService.getUserByCustomerNumber(customerNumber);
        if (user == null) {
            return new UserInfoRes();
        }
        UserInfoRes res = ReflectionUtil.copy(user, UserInfoRes.class);
        res.setRealName(StringUtils.isNotBlank(user.getIdCardNum()) ? CommonConst.YES : CommonConst.NO);
        res.setBindWechat(StringUtils.isNotBlank(user.getWechatId()) ? CommonConst.YES : CommonConst.NO);
        res.setWechatUserId(StringUtils.equals(user.getWechatId(), user.getWechatId()) ? CommonConst.YES : CommonConst.NO);
        return res;
    }

    @Override
    public void fillUserInfo(FillUserInfoReq req) {
        String customerNumber = tokenService.getItem(TokenItemConst.OPERATOR.name(), String.class);
        AcUser user = acUserService.getUserByCustomerNumber(customerNumber);
        Validator.assertNotNull(user, ErrCodeSys.PA_DATA_NOT_EXIST, "用户");
        Validator.assertTrue(StringUtils.equals(req.getPassword(), req.getPasswordConfirm()), ErrCodeSys.PA_DATA_DIFF, "密码");
        fillUserInfo(req, user);
        acUserService.updateByPrimaryKeySelective(user);

    }

    private void fillUserInfo(FillUserInfoReq req, AcUser user) {
        Date now = new Date();
        user.setName(req.getName());
        user.setEmail(req.getEmail());
        user.setPhone(req.getPhone());
        String password = req.getPassword();
        user.setPassword(MD5Util.generate(password));
        user.setPasswordUpdateLastTime(now);
        user.setPasswordErrorTimes(0);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void realName(RealNameReq req) {
        String customerNumber = tokenService.getItem(TokenItemConst.OPERATOR.name(), String.class);
        AcUser user = acUserService.getUserByCustomerNumber(customerNumber);
        Validator.assertNotNull(user, ErrCodeSys.PA_DATA_NOT_EXIST, "用户");
        Validator.assertTrue(StringUtils.isBlank(user.getIdCardNum()), ErrCodeSys.SYS_ERR_MSG, "已实名");
        user.setIdCardNum(req.getId());
        user.setName(req.getName());
        acUserService.updateByPrimaryKey(user);
    }

    @Override
    public void setAvatar(String avatar) {
        String customerNumber = tokenService.getItem(TokenItemConst.OPERATOR.name(), String.class);
        setAvatar(customerNumber, avatar);
    }

    @Override
    public void setAvatar(String customerNumber, String avatar) {
        AcUser user = acUserService.getUserByCustomerNumber(customerNumber);
        Validator.assertNotNull(user, ErrCodeSys.PA_DATA_NOT_EXIST, "用户");
        user.setAvatar(avatar);
        acUserService.updateByPrimaryKeySelective(user);
    }
}
